﻿Imports System.Data.SqlClient

Public Class clsCONDUCTOR
    Private mstrID_CONDUCTOR As String
    Private mstrCOD_CONDUCTOR As String
    Private mstrNOMBRE As String
    Private mstrAPELLIDO1 As String
    Private mstrAPELLIDO2 As String
    Private mstrID_SECTOR As String
    Private mstrACTIVO As String
    Private mintID_CONDUCTOR_DVR As Integer
    Public Property ID_CONDUCTOR() As String
        Get
            Return mstrID_CONDUCTOR
        End Get
        Set(ByVal value As String)
            mstrID_CONDUCTOR = value
        End Set
    End Property
    Public Property COD_CONDUCTOR() As String
        Get
            Return mstrCOD_CONDUCTOR
        End Get
        Set(ByVal value As String)
            mstrCOD_CONDUCTOR = value
        End Set
    End Property
    Public Property NOMBRE() As String
        Get
            Return mstrNOMBRE
        End Get
        Set(ByVal value As String)
            mstrNOMBRE = value
        End Set
    End Property
    Public Property APELLIDO1() As String
        Get
            Return mstrAPELLIDO1
        End Get
        Set(ByVal value As String)
            mstrAPELLIDO1 = value
        End Set
    End Property
    Public Property APELLIDO2() As String
        Get
            Return mstrAPELLIDO2
        End Get
        Set(ByVal value As String)
            mstrAPELLIDO2 = value
        End Set
    End Property
    Public Property ID_SECTOR() As String
        Get
            Return mstrID_SECTOR
        End Get
        Set(ByVal value As String)
            mstrID_SECTOR = value
        End Set
    End Property
    Public Property ACTIVO() As String
        Get
            Return mstrACTIVO
        End Get
        Set(ByVal value As String)
            mstrACTIVO = value
        End Set
    End Property

    Public Property ID_CONDUCTOR_DVR() As Integer
        Get
            Return mintID_CONDUCTOR_DVR
        End Get
        Set(ByVal value As Integer)
            mintID_CONDUCTOR_DVR = value
        End Set
    End Property

    Public Sub New()
        mstrID_CONDUCTOR = ""
        mstrCOD_CONDUCTOR = ""
        mstrNOMBRE = ""
        mstrAPELLIDO1 = ""
        mstrAPELLIDO2 = ""
        mstrID_SECTOR = ""
        mstrACTIVO = ""
        mintID_CONDUCTOR_DVR = 0
    End Sub
    Public Sub Insertar()
        Dim sql As String = "Insert Into CONDUCTOR (" & _
                            "ID_CONDUCTOR," & _
                            "COD_CONDUCTOR," & _
                            "NOMBRE," & _
                            "APELLIDO1," & _
                            "APELLIDO2," & _
                            "ID_SECTOR," & _
                            "ACTIVO," & _
                            "ID_CONDUCTOR_DVR)" & _
                            "VALUES (" & _
                            "SUBSTRING(@ID_CONDUCTOR,1,100)," & _
                            "SUBSTRING(@COD_CONDUCTOR,1,150)," & _
                            "SUBSTRING(@NOMBRE,1,50)," & _
                            "SUBSTRING(@APELLIDO1,1,50)," & _
                            "SUBSTRING(@APELLIDO2,1,50)," & _
                            "SUBSTRING(@ID_SECTOR,1,50)," & _
                            "SUBSTRING(@ACTIVO,1,1)," & _
                            "@ID_CONDUCTOR_DVR)"

        Dim cnConn As New SqlConnection
        cnConn.ConnectionString = OptocontrolConnection

        Dim Command As New SqlCommand(sql, cnConn)

        Command.Parameters.AddWithValue("ID_CONDUCTOR", mstrID_CONDUCTOR)
        Command.Parameters.AddWithValue("COD_CONDUCTOR", mstrCOD_CONDUCTOR)
        Command.Parameters.AddWithValue("NOMBRE", mstrNOMBRE)
        Command.Parameters.AddWithValue("APELLIDO1", mstrAPELLIDO1)
        Command.Parameters.AddWithValue("APELLIDO2", mstrAPELLIDO2)
        Command.Parameters.AddWithValue("ID_SECTOR", mstrID_SECTOR)
        Command.Parameters.AddWithValue("ACTIVO", mstrACTIVO)
        Command.Parameters.AddWithValue("ID_CONDUCTOR_DVR", mintID_CONDUCTOR_DVR)

        cnConn.Open()
        Dim RowsAffected As Integer
        Try
            RowsAffected = Command.ExecuteNonQuery
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Aviso")
        End Try

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible insertar el registro de conductor", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If
    End Sub
    Public Sub Actualizar()

        Dim strSqlQry As String
        Dim cnConn As New SqlConnection

        cnConn.ConnectionString = OptocontrolConnection
        strSqlQry = "Update CONDUCTOR Set " & _
                    "COD_CONDUCTOR=SUBSTRING(@COD_CONDUCTOR,1,150)," & _
                    "NOMBRE=SUBSTRING(@NOMBRE,1,50)," & _
                    "APELLIDO1=SUBSTRING(@APELLIDO1,1,50)," & _
                    "APELLIDO2=SUBSTRING(@APELLIDO2,1,50)," & _
                    "ID_SECTOR=SUBSTRING(@ID_SECTOR,1,100), " & _
                    "ACTIVO=SUBSTRING(@ACTIVO,1,1), " & _
                    "ID_CONDUCTOR_DVR=@ID_CONDUCTOR_DVR " & _
                    "Where ID_CONDUCTOR=@ID_CONDUCTOR"


        Dim Command As New SqlCommand(strSqlQry, cnConn)

        Command.Parameters.AddWithValue("ID_CONDUCTOR", mstrID_CONDUCTOR)
        Command.Parameters.AddWithValue("COD_CONDUCTOR", mstrCOD_CONDUCTOR)
        Command.Parameters.AddWithValue("NOMBRE", mstrNOMBRE)
        Command.Parameters.AddWithValue("APELLIDO1", mstrAPELLIDO1)
        Command.Parameters.AddWithValue("APELLIDO2", mstrAPELLIDO2)
        Command.Parameters.AddWithValue("ID_SECTOR", mstrID_SECTOR)
        Command.Parameters.AddWithValue("ACTIVO", mstrACTIVO)
        Command.Parameters.AddWithValue("ID_CONDUCTOR_DVR", mintID_CONDUCTOR_DVR)
        cnConn.Open()

        Dim RowsAffected As Integer = Command.ExecuteNonQuery

        If RowsAffected > 0 Then
            cnConn.Close()
        Else
            MsgBox("No fue posible actualizar el conductor ", MsgBoxStyle.Exclamation, "Aviso")
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End If





    End Sub
End Class
